package com.example.base.dict;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.base.system.domain.SysDicCategory;
import com.example.base.system.domain.SysDicData;
import com.example.base.system.service.SysDicCategoryService;
import com.example.base.system.service.SysDicDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private SysDicCategoryService sysDicCategoryService;

    @Autowired
    private SysDicDataService sysDicDataService;

    @Override
    public JSONArray getData(String category) {
        LambdaQueryWrapper<SysDicCategory> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDicCategory::getCategoryNo,category);
        SysDicCategory dicCategory=sysDicCategoryService.getOne(lambdaQueryWrapper);
        if(dicCategory==null){
            return null;
        }
        LambdaQueryWrapper<SysDicData> sysDicDataLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysDicDataLambdaQueryWrapper.eq(SysDicData::getDicCategoryId,dicCategory.getId());
        List<SysDicData> dicDataList=sysDicDataService.list(sysDicDataLambdaQueryWrapper);
        if(dicDataList!=null && dicDataList.size()>0){
            JSONArray array=new JSONArray();
            JSONObject object=null;
            for (SysDicData sysDicData: dicDataList){
                object=new JSONObject();
                object.put("key",sysDicData.getDicKey());
                object.put("value",sysDicData.getDicValue());
                object.put("label",sysDicData.getDicName());
                array.add(object);
            }
            return array;
        }
        return null;
    }

    @Override
    public JSONObject getData(String category, String key) {
        LambdaQueryWrapper<SysDicCategory> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDicCategory::getCategoryNo,category);
        SysDicCategory dicCategory=sysDicCategoryService.getOne(lambdaQueryWrapper);
        if(dicCategory==null){
            return null;
        }
        LambdaQueryWrapper<SysDicData> sysDicDataLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysDicDataLambdaQueryWrapper.eq(SysDicData::getDicCategoryId,dicCategory.getId());
        sysDicDataLambdaQueryWrapper.eq(SysDicData::getDicKey,key);
        SysDicData sysDicData=sysDicDataService.getOne(sysDicDataLambdaQueryWrapper);
        JSONObject object=new JSONObject();
        object.put("key",sysDicData.getDicKey());
        object.put("value",sysDicData.getDicValue());
        object.put("label",sysDicData.getDicName());
        return object;
    }

    @Override
    public String getDataValue(String category, String key) {
        LambdaQueryWrapper<SysDicCategory> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDicCategory::getCategoryNo,category);
        SysDicCategory dicCategory=sysDicCategoryService.getOne(lambdaQueryWrapper);
        if(dicCategory==null){
            return null;
        }
        LambdaQueryWrapper<SysDicData> sysDicDataLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysDicDataLambdaQueryWrapper.eq(SysDicData::getDicCategoryId,dicCategory.getId());
        sysDicDataLambdaQueryWrapper.eq(SysDicData::getDicKey,key);
        SysDicData sysDicData=sysDicDataService.getOne(sysDicDataLambdaQueryWrapper);
        return sysDicData.getDicValue();
    }

    @Override
    public JSONArray getCategoryList(String parentCategory) {
        LambdaQueryWrapper<SysDicCategory> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDicCategory::getCategoryNo,parentCategory);
        SysDicCategory dicCategory=sysDicCategoryService.getOne(lambdaQueryWrapper);
        if(dicCategory==null){
            return null;
        }
        LambdaQueryWrapper<SysDicCategory> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDicCategory::getPid,dicCategory.getId());
        List<SysDicCategory> list=sysDicCategoryService.list(queryWrapper);
        if(list!=null && list.size()>0){
            JSONArray array=new JSONArray();
            JSONObject object=null;
            for (SysDicCategory category: list){
                object=new JSONObject();
                object.put("key",category.getCategoryNo());
                object.put("label",category.getCategoryName());
                array.add(object);
            }
            return array;
        }
        return null;
    }
}
